package com.sky.controller.admin;


import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;

import com.sky.service.FavorService;
import com.sky.service.SetmealDishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.naming.spi.DirStateFactory;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
public class DishController {
    private static final Logger log = LoggerFactory.getLogger(DishController.class);
    @Autowired
    DishService dishService;
    @Autowired
    FavorService favorService;
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping()
    public Result Savedishwithflavor(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishService.save(dish);
        Long dishId =  dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        favorService.save(flavors);

        return Result.success();

    }

    @GetMapping("/page")
    public Result<PageResult> SelectByPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult =  dishService.SelectBypage(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping("/status/{status}")
    public Result StartOrclose(@PathVariable String status, String id){
           log.info("对菜品进行停售或起售操作：{}",status);
           dishService.StartOrclose(Integer.valueOf(status),Long.valueOf(id));
           return Result.success();
    }
    @DeleteMapping()
    public Result Delete(@RequestParam(name = "ids") List<Long> list){
          log.info("批量删除：{}",list);
          //删除菜品表中的菜品信息
          dishService.Delete(list);
          //删除口味表中菜品对应的信息
          favorService.DeleteByDishid(list);
          //删除套餐菜品关系表中对应的信息
          setmealDishService.DeleteByDishId(list);
          //将对应的缓存全部删除
          redisTemplate.delete("dish_*");

          return Result.success();


    }
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品信息：{}",id);
        DishVO dishVo = dishService.getById(id);
        return Result.success(dishVo);
    }

    //修改菜品信息
    @PutMapping()
    public Result update(@RequestBody DishDTO dishDTO){
        //修改菜品表中的菜品信息
        dishService.Update(dishDTO);
        //删除口味表中该菜品对应的口味信息
        List<Long> list =new  ArrayList<Long>();
        list.add(dishDTO.getId());
        favorService.DeleteByDishid(list);
        //将更新后的口味信息插入口味表中
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        for(int i = 0; i < dishFlavorList.size(); i++){
             dishFlavorList.get(i).setDishId(dishDTO.getId());
        }
        log.info("口味表菜品ID:{}",dishFlavorList);
        if(dishFlavorList!=null && dishFlavorList.size()>0){
        favorService.save(dishFlavorList);
        log.info("口味表菜品ID:{}",dishFlavorList);}
        return Result.success();

    }
   @GetMapping("/list")
    public Result<List<Dish>> GetDishesByCategoryId(@RequestParam Long categoryId){
         log.info("根据分类id查询菜品：{}",categoryId);

         List<Dish> dishList =  dishService.GetDishesByCategoryId(categoryId);
         return Result.success(dishList);

   }






}
